const el = document.getElementsByTagName('html')[0]
let scrollTop = 0

export function hasClass (className) {
  let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
  return reg.test(el.className)
}

export function addClass (className) {
  if (hasClass(className)) return
  scrollTop = document.scrollingElement.scrollTop
  localStorage.setItem('top', scrollTop)
  el.style.top = `-${scrollTop}px`
  if (el.className === '') {
    el.className += className
  } else {
    let newClass = el.className.split(' ')
    newClass.push(className)
    el.className = newClass.join(' ')
  }
}

export function removeClass (className) {
  if (hasClass(className)) {
    el.className = el.className.replace(new RegExp('(\\s|^)' + className + '(\\s|$)'), '')
    document.scrollingElement.scrollTop = scrollTop
  };
}